<?php

/**
 * This is the model class for table "package".
 *
 * The followings are the available columns in table 'package':
 * @property string $id
 * @property string $name
 * @property string $file
 * @property string $desc
 */
class Package extends CActiveRecord
{       
        const XML_PACKAGE_PATH = '/media/xml/';
	/**
	 * Returns the static model of the specified AR class.
	 * @return Package the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'package';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, file', 'required'),
			array('name, file', 'length', 'max'=>255),
                        array('name', 'unique', 'message' => "This package's name already exists."),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, file, desc', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'file' => 'File',
			'desc' => 'Desc',
                        'cate_id' => 'Category',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($cate_id = null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
                if($cate_id!=null)
                $criteria->addCondition('cate_id = '.$cate_id);
		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('file',$this->file,true);
		$criteria->compare('desc',$this->desc,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        public static function convertVietNgu($string) {
            $pattern = array(
                "'á'","'à'","'ã'","'ả'","'ạ'","'ă'","'ắ'","'ằ'","'ẵ'","'ặ'","'ẳ'","'â'","'ấ'","'ầ'","'ẫ'","'ậ'","'ẩ'",
                "'Á'","'À'","'Ã'","'Ả'","'Ạ'","'Ă'","'Ắ'","'Ằ'","'Ẵ'","'Ặ'","'Ẳ'","'Â'","'Ấ'","'Ầ'","'Ẫ'","'Ậ'","'Ẩ'",
                 "'đ'","'Đ'",
                 "'é'","'è'","'ẽ'","'ẹ'","'ẻ'","'ê'","'ế'","'ề'","'ễ'","'ệ'","'ể'",
                "'É'","'È'","'Ẽ'","'Ẹ'","'Ẻ'","'Ê'","'Ế'","'Ề'","'Ễ'","'Ệ'","'Ể'",
                 "'í'","'ì'","'ĩ'","'ị'","'ỉ'",
                "'Í'","'Ì'","'Ĩ'","'Ị'","'Ỉ'",
                 "'ó'","'ò'","'õ'","'ọ'","'ỏ'","'ô'","'ố'","'ồ'","'ỗ'","'ộ'","'ổ'","'ơ'","'ớ'","'ờ'","'ỡ'","'ở'","'ợ'",
                "'Ó'","'Ò'","'Õ'","'Ọ'","'Ỏ'","'Ô'","'Ố'","'Ồ'","'Ỗ'","'Ộ'","'Ổ'","'Ơ'","'Ớ'","'Ờ'","'Ỡ'","'Ở'","'Ợ'",
                 "'ú'","'ù'","'ũ'","'ủ'","'ụ'","'ư'","'ứ'","'ừ'","'ữ'","'ử'","'ự'",
                "'Ú'","'Ù'","'Ũ'","'Ủ'","'Ụ'","'Ư'","'Ứ'","'Ừ'","'Ữ'","'Ử'","'Ự'",
                 "'ý'","'ỳ'","'ỹ'","'ỷ'","'ỵ'",
                "'Ý'","'Ỳ'","'Ỹ'","'Ỷ'","'Ỵ'",
                 
             );
             $replace = array(
                 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
                 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
                 'd','d',
                 'e','e','e','e','e','e','e','e','e','e','e',
                 'e','e','e','e','e','e','e','e','e','e','e',
                 'i','i','i','i','i',
                 'i','i','i','i','i',
                 'o','o','o','o','o','o','o','o','o','o','o','o','o','o','o','o','o',
                 'o','o','o','o','o','o','o','o','o','o','o','o','o','o','o','o','o',
                 'u','u','u','u','u','u','u','u','u','u','u',
                 'u','u','u','u','u','u','u','u','u','u','u',
                 'y','y','y','y','y',
                 'y','y','y','y','y',
                 );
                 //   print_r($cate);
             $output = strtolower($string);
             $output = preg_replace($pattern,$replace,$output); 
             $output = preg_replace("/[^A-Za-z0-9_]/","",$output);
             //$output = preg_replace("/,/","",$output); 
             return $output;
        }
        
        public function readPackage($amount = 1){
            $packages = simplexml_load_file(Yii::getPathOfAlias('webroot').self::XML_PACKAGE_PATH.$this->file.'.xml');
            $products = array();
            foreach ($packages as $package){
                if ($package->getName() == 'pack'){
                    $pack = Package::model()->findByPk($package->id);
                    foreach($pack->readPackage($package->amount) as $p){
                        $val = 1;
                        if ($products != null)
                            foreach($products as $product){
                                if (0 == strcmp($p->id, $product->id)){
                                    $product->amount = $product->amount + $p->amount;
                                    $val = 0;
                                }
                            }
                            if ($val == 1)
                                $products[] = $p;
                    }
                }
                if ($package->getName() == 'product'){
                    $value = 1;
                    foreach ($products as $pro){
                        if ($pro->id == $package->id){
                            $pro->amount = $pro->amount + $package->amount * $amount;
                            $value = 0;
                        }
                    }
                    if ($value == 1){
                        $package->amount = $package->amount * $amount;
                        $products[] = $package;
                    }
                }         
            }
            return $products;
        }
}